package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	context2 "golang.org/x/net/context"
	"ksd-social-api/commons/page"
	"ksd-social-api/modules/note/model"
)

/**
 * 用户点赞
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
type NoteUserLikeMapper struct {
}

/**
 * 点赞
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 */
func (service *NoteUserLikeMapper) SaveNoteUserLike(noteId uint64, noteUserLike model.NoteUserLike) bool {
	// 创建orm对象
	err := orm.NewOrm().DoTx(func(ctx context2.Context, txOrm orm.TxOrmer) (err error) {
		_, err = txOrm.Insert(&noteUserLike)
		_, err = txOrm.Raw("update  xk_note set like_num = like_num + 1 ,update_time = now() where id = ? and like_num >= 0 and system_id = ?", noteId, noteUserLike.SystemId).Exec()
		return err
	})
	return nil == err
}

/**
 * 查询是否点赞
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *NoteUserLikeMapper) CountNoteUserLike(userId uint64, noteId uint64, systemId uint64) int64 {
	// 创建orm对象
	mysql := orm.NewOrm()
	var total int64
	err := mysql.Raw("SELECT is_focus FROM xk_note_user_like WHERE user_id = ? AND opid = ? and system_id = ?", userId, noteId, systemId).QueryRow(&total)
	if nil != err {
		return -1
	}
	return total
}

/**
 * 取消点赞
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 */
func (service *NoteUserLikeMapper) CancelNoteUserLike(userId uint64, noteId uint64, systemId uint64) bool {
	// 创建orm对象
	err := orm.NewOrm().DoTx(func(ctx context2.Context, txOrm orm.TxOrmer) (err error) {
		_, err = txOrm.Raw("UPDATE xk_note_user_like SET is_focus = 0 ,update_time = now() WHERE user_id = ? AND opid = ? and system_id = ?", userId, noteId, systemId).Exec()
		_, err = txOrm.Raw("update  xk_note set like_num = like_num - 1  ,update_time = now() where id = ? and like_num > 0 and system_id = ?", noteId, systemId).Exec()
		return err
	})
	return err == nil
}

/**
 * 恢复点赞
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 */
func (service *NoteUserLikeMapper) RecoverNoteUserLike(userId uint64, noteId uint64, systemId uint64) bool {
	// 创建orm对象
	err := orm.NewOrm().DoTx(func(ctx context2.Context, txOrm orm.TxOrmer) (err error) {
		_, err = txOrm.Raw("UPDATE xk_note_user_like SET is_focus = 1  ,update_time = now() WHERE user_id = ? AND opid = ? and system_id = ?", userId, noteId, systemId).Exec()
		_, err = txOrm.Raw("update  xk_note set like_num = like_num + 1   ,update_time = now() where id = ? and like_num >= 0 and system_id = ?", noteId, systemId).Exec()
		return err
	})
	return err == nil
}

/**
 * 删除点赞
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 */
func (service *NoteUserLikeMapper) DelNoteUserLike(id uint64, userId uint64, noteId uint64, systemId uint64) bool {
	// 创建orm对象
	err := orm.NewOrm().DoTx(func(ctx context2.Context, txOrm orm.TxOrmer) (err error) {
		_, err = txOrm.Raw("delete from xk_note_user_like where id = ? and user_id = ? AND opid = ? and system_id = ?", id, userId, noteId, systemId).Exec()
		_, err = txOrm.Raw("update  xk_note set like_num = like_num - 1   ,update_time = now() where id = ? and like_num > 0 and system_id = ?", noteId, systemId).Exec()
		return err
	})
	return err == nil
}

/**
* 用户点赞课程
* @author feige
* @date 2023-11-03
* @version 1.0
* @desc
 */
func (mapper *NoteUserLikeMapper) FindNoteUserLikePage(userId uint64, channelId uint64, systemId uint64, pageNo int64, pageSize int64) (p *page.Page, err error) {
	// 准备容器对象，开始装载数据库数据
	noteList := []model.Note{}
	//创建orm对象
	mysql := orm.NewOrm()

	var total int64 = 0
	// 开始执行sql查询
	err = mysql.Raw(`select count(1) from xk_note_user_like t2,xk_note t1 where t1.id = t2.opid and t2.is_focus= 1 and t1.status = 1 and t1.is_deleted = 0 and t2.user_id = ? and t2.system_id = ? and t1.channel_id = ?`, userId, systemId, channelId).QueryRow(&total)
	if err != nil {
		return nil, err
	}
	// 重新换算分页和规则
	page := p.Page(pageNo, pageSize, total)
	// 执行count查询
	// 开始执行sql查询
	_, err2 := mysql.Raw(`
		select 
		t1.id,
		t1.title,
		t1.tags,
		t1.pan_link,
		t1.pan_pwd,
		t1.description,
		t1.viewcount,
		t1.create_time,
		t1.update_time,
		t1.comment_flag,
		t1.avatar,
		t1.username,
		t1.status,
		t1.userid,
		t1.ntype,
		t1.img,
		t1.fav_num,
		t1.comment_num,
		t1.price,
		t1.realprice,
		t1.new_flag,
		t1.hot_flag,
		t1.push_flag,
		t1.channel_id,
		t1.channel_name,
		t1.category_id,
		t1.category_name,
		t1.uuid,
		t1.chapter_flag,
		t1.notetype,
		t1.vip_flag,
		t1.buy_num,
		t1.stimer,
		t1.finished,
		t1.pay_flag
		from xk_note_user_like t2,xk_note t1 
		where t1.id = t2.opid and t2.is_focus= 1 and t1.status = 1 and t1.is_deleted = 0 and t2.user_id = ? and t2.system_id = ? and t1.channel_id = ?
		order by t2.update_time desc 
		limit ?,?`,
		userId, systemId, channelId, page.CurrentPage, page.PageSize).QueryRows(&noteList)

	// 6：把查询的数据放入到分页的records字段，准备返回
	page.Records = noteList
	// 7: 如果没有找到直接返回
	if err2 != nil {
		return nil, err2
	}
	return page, nil
}
